﻿#region Licence
// <copyright file="Core/ThemeManager.cs" company="Kopigi">
// Copyright © Kopigi 2014
// </copyright>
// ****************************************************************************
// <author>Marc PLESSIS</author>
// <date>11/10/2014</date>
// <project>Nentang.wpf</project>
// <web>http://nentang.codeplex.com</web>
// <license>
// The MIT License (MIT)
// 
// Copyright (c) 2013 Marc Plessis
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// 
// </license>
#endregion
#region using

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Media;
using Core.Enums;
using Helpers;

#endregion

namespace Core.Class
{
    /// <summary>
    /// Permet de gérer le choix des thémes de l'application
    /// </summary>
    public static class ThemeManager
    {
        #region fields

        private static readonly Dictionary<ThemesEnum, ThemeChoice> ThemesDictionary =
            new Dictionary<ThemesEnum, ThemeChoice>();

        private static readonly Dictionary<Color, AccentColorChoice> AccentColorDictionary =
            new Dictionary<Color, AccentColorChoice>();

        private static ThemeChoice _actualTheme;
        private static AccentColorChoice _actualAccentColor;

        #endregion

        /// <summary>
        /// Theme en cours
        /// </summary>
        public static ThemeChoice ActualTheme
        {
            get { return _actualTheme; }
        }

        /// <summary>
        /// Couleur d'accentuation en cours
        /// </summary>
        public static AccentColorChoice ActualAccentColor
        {
            get { return _actualAccentColor; }
        }

        /// <summary>
        /// Préparation du dictionnaire des thémes
        /// </summary>
        public static void InitializeThemes()
        {
            ThemesDictionary.Clear();
            ThemesDictionary.Add(ThemesEnum.Dark, new ThemeChoice { Name = "DarkTheme", Theme = ThemesEnum.Dark, UriTheme = new Uri("/UI;component/Themes/Dark.xaml", UriKind.RelativeOrAbsolute)});
            ThemesDictionary.Add(ThemesEnum.Light, new ThemeChoice { Name = "LightTheme", Theme = ThemesEnum.Light, UriTheme = new Uri("/UI;component/Themes/Light.xaml", UriKind.RelativeOrAbsolute) });

            AccentColorDictionary.Clear();
            AccentColorDictionary.Add(ColorsHelper.HexToColor("#FF008dd0"), new AccentColorChoice { AccentColor = ColorsHelper.HexToColor("#FF008dd0"), UriTheme = new Uri("/UI;component/AccentColors/BlueColor.xaml", UriKind.RelativeOrAbsolute)});
        }

        /// <summary>
        /// Permet de renvoyer la liste des thémes de l'application
        /// </summary>
        /// <returns></returns>
        public static List<ThemeChoice> GetThemes()
        {
            return ThemesDictionary.Select(choice => choice.Value).ToList();
        }

        /// <summary>
        /// Application d'un théme
        /// </summary>
        /// <param name="theme"></param>
        public static void SetTheme(ThemesEnum theme)
        {            
            try
            {
                ThemeResourceDictionary themeDictionary = null;
                var themeSelected = ThemesDictionary[theme] ?? ThemesDictionary.First().Value;

                themeDictionary = new ThemeResourceDictionary
                {
                    Source = (Uri)themeSelected.UriTheme
                };
                _actualTheme = themeSelected;

                var existingDictionaries = (from dictionary in Application.Current.Resources.MergedDictionaries.OfType<ThemeResourceDictionary>() select dictionary).ToList();

                foreach (var thDictionary in existingDictionaries)
                {
                    if (themeDictionary != thDictionary)
                    {
                        Application.Current.Resources.MergedDictionaries.Remove(thDictionary);    
                    }
                }
                Application.Current.Resources.MergedDictionaries.Add(themeDictionary);
            }
            finally { }
        }

        /// <summary>
        /// Préparation du dictionnaire des couleurs d'accentuation
        /// </summary>
        public static void InitializeAccentColors()
        {
            AccentColorDictionary.Clear();
            AccentColorDictionary.Add(ColorsHelper.HexToColor("#FF71b1d1"), new AccentColorChoice { AccentColor = ColorsHelper.HexToColor("#FF71b1d1"), UriTheme = new Uri("/UI;component/AccentColors/BlueColor.xaml", UriKind.RelativeOrAbsolute) });
            AccentColorDictionary.Add(ColorsHelper.HexToColor("#FF11b520"), new AccentColorChoice { AccentColor = ColorsHelper.HexToColor("#FF11b520"), UriTheme = new Uri("/UI;component/AccentColors/GreenColor.xaml", UriKind.RelativeOrAbsolute) });
            AccentColorDictionary.Add(ColorsHelper.HexToColor("#FFd07a00"), new AccentColorChoice { AccentColor = ColorsHelper.HexToColor("#FFd07a00"), UriTheme = new Uri("/UI;component/AccentColors/OrangeColor.xaml", UriKind.RelativeOrAbsolute) });
        }

        /// <summary>
        /// Permet de renvoyer la liste des couleurs d'accentuation
        /// </summary>
        /// <returns></returns>
        public static List<AccentColorChoice> GetAccentColors()
        {
            return AccentColorDictionary.Select(choice => choice.Value).ToList();
        }

        /// <summary>
        /// Application d'une couleur d'accentuation
        /// </summary>
        /// <param name="accentColor"></param>
        public static void SetAccentColor(Color accentColor)
        {
            try
            {
                AccentColorResourceDictionary accentColorsDictionary = null;
                var accentColorSelected = AccentColorDictionary[accentColor] ?? AccentColorDictionary.First().Value;

                accentColorsDictionary = new AccentColorResourceDictionary
                {
                    Source = (Uri)accentColorSelected.UriTheme
                };
                _actualAccentColor = accentColorSelected;

                var existingDictionaries = (from dictionary in Application.Current.Resources.MergedDictionaries.OfType<AccentColorResourceDictionary>() select dictionary).ToList();

                foreach (var acDictionary in existingDictionaries)
                {
                    if (accentColorsDictionary != acDictionary)
                    {
                        Application.Current.Resources.MergedDictionaries.Remove(acDictionary);
                    }
                }
                Application.Current.Resources.MergedDictionaries.Add(accentColorsDictionary);
            }
            finally { }
        }
    }

    /// <summary>
    /// Permet de typer le dictionnaire de ressources du théme
    /// </summary>
    public class ThemeResourceDictionary : ResourceDictionary
    {}

    /// <summary>
    /// Permet de typer le dictionnaire de ressources du théme
    /// </summary>
    public class AccentColorResourceDictionary : ResourceDictionary
    { }

    /// <summary>
    /// Permet de choisir un théme
    /// </summary>
    public class ThemeChoice
    {
        /// <summary>
        /// Nom du théme
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// Clé du théme
        /// </summary>
        public ThemesEnum Theme { get; set; }

        /// <summary>
        /// Uri du Théme
        /// </summary>
        public Uri UriTheme { get; set; }
    }

    /// <summary>
    /// Permet de choisir un théme
    /// </summary>
    public class AccentColorChoice
    {
        /// <summary>
        /// Nom du théme
        /// </summary>
        public Color AccentColor { get; set; }

        /// <summary>
        /// Uri du Théme
        /// </summary>
        public Uri UriTheme { get; set; }
    }
}
